<template>
  <div style="width: 97%;margin: 0 auto">
    <div class="search">
      <div style="margin-left: 16px;margin-top: 10px;">
        <i class="el-icon-s-custom" ></i>添加处方药
      </div>
      <div class="int">
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
          <input type="text"  placeholder="药品名称" v-model="queryParams.name" style="height: 30px;width: 252px">
          <el-date-picker
            style="width: 252px;height: 30px!important;margin-left: 20px;margin-top: 20px"
            v-model="queryParams.time"
            size="small"
            type="daterange"
            range-separator=" "
            start-placeholder="创建日期"
            end-placeholder="">
          </el-date-picker>
        </el-form>
      </div>
      <div class="inquire">
        <el-button type="success" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="list">
      <div style="margin-left: 10px;margin-top: 10px;margin-bottom: 10px;overflow:hidden;">
        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:drug:add']"
          >新增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            style="margin-left: 20px; width: 80px;height: 32px"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </el-col>
      </div>
      <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">

        <el-table-column
          type="selection"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="title"
          label="药品名称"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="strength"
          label="用药强度"
          align="center"
          width="150">
        </el-table-column>
        <el-table-column
          prop="drugUnitName"
          label="单元"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="frequency"
          label="频度"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="drugNum"
          label="频次"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="eatDrugTimesJson"
          label="剂量"
          align="center"
          width="120">
        </el-table-column>
        <el-table-column
          prop="eatDrug"
          label="用药时间"
          align="center"
          width="200">
        </el-table-column>
        <el-table-column
          prop="eatDrugStartTime"
          label="开始时间"
          align="center"
          width="200">
        </el-table-column>
        <el-table-column
          prop="eatDrugEndTime"
          label="结束时间"
          align="center"
          width="">
        </el-table-column>
      </el-table>
      <!--      <div style="position: absolute;right: 30px;bottom: 20px;">-->

      <div style="position: fixed;right: 30px;bottom: 60px;">

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="druglist"
        />
        <!--        <el-pagination-->
        <!--          @size-change="handleSizeChange"-->
        <!--          @current-change="handleCurrentChange"-->
        <!--          :current-page="currentPage"-->
        <!--          :page-sizes="[3, 5, 10]"-->
        <!--          :page-size="pagesize"-->
        <!--          layout=" prev, pager, next,sizes, jumper"-->
        <!--          :total="tableData.length">-->
        <!--        </el-pagination>-->
      </div>
      <!--    修改-->
      <!-- 修改参数配置对话框 -->
      <div class="alter" v-show="fage">
        <div class="hint">
          <div style="float:left;margin-left: 20px;">
            <i class="el-icon-s-custom" style="float:left;"></i>
            <p style="margin-left: -10px;margin-top: 0px;">添加处方药</p>
          </div>
          <i class="el-icon-close" @click="abrogate"></i></div>
        <div style="width: 35%;margin: 0 auto">
          <el-form ref="form"  label-width="80px" :model="form" >
            <el-form-item label="用药名称" prop="medication" style="margin-top: 30px;margin-left: 40px" size="small">
              <el-input class="txt" v-model="form.title" style="width: 300px;" placeholder="请选择药品名称"></el-input>
            </el-form-item>
            <el-form-item label="用药强度" prop="strength" style="margin-left: 40px" size="small">
              <el-input class="txt" v-model="form.strength" style="width: 300px;" placeholder="请选择输入药品强度"></el-input>
            </el-form-item>
            <el-form-item label="单元" prop="unit" style="margin-left: 40px" size="small">
              <el-select v-model="form.drugUnitName" placeholder="请选择药品单元" style="width: 300px;"  size="small">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="频度" prop="frequentness" style="margin-left: 40px" size="small" >
              <el-select v-model="form.frequency" placeholder="请选择药品频度" style="width: 300px;"  size="small"                   @click="pin"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="间隔天数" prop="frequentness" style="margin-left: 40px" size="small" v-show="days">
              <el-select v-model="form.separateDay" placeholder="请选择药品频度" style="width: 300px;"  size="small">
                <el-option
                  v-for="item in numberdays"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="频次" prop="frequency" style="margin-left: 40px" size="small">
              <el-select v-model="form.drugNum" placeholder="请选择药品频次" style="width: 300px;"  size="small">
                <el-option
                  v-for="item in optionsfry"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="剂量" prop="dosage" style="margin-left: 40px" size="small">
              <el-input class="txt" v-model="form.eatDrugTimesJson" style="width: 300px;" placeholder="选择剂量"></el-input>
            </el-form-item>
            <el-form-item label="用药时间" prop="time" style="margin-left: 40px;" size="small">
              <el-time-select
                style="width: 300px"
                v-model="form.eatDrug"
                :picker-options="{
                start: '08:30',
                step: '00:15',
                 end: '18:30'
              }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
            <el-form-item label="开始时间" prop="begin" style="margin-left: 40px" size="small">
              <el-time-select
                style="width: 300px"
                v-model="form.eatDrugStartTime"
                :picker-options="{
                start: '08:30',
                step: '00:15',
                 end: '18:30'
              }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
            <el-form-item label="结束时间" prop="end" style="margin-left: 40px" size="small">
              <el-time-select
                style="width: 300px"
                v-model="form.eatDrugEndTime"
                :picker-options="{
                start: '08:30',
                step: '00:15',
                 end: '18:30'
              }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
            <div style="width: 200px;margin: 0 auto">
              <el-button type="success" style="height: 30px;display: flex;align-items: center;float:left;margin-right: 20px" @click="notarize">
                确认</el-button>
              <el-button style="height: 30px;display: flex;align-items: center;" @click="abrogate">
                取消</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getDetail,addDrug, updateDrug, delDrug ,importTemplate  } from "../../../api/drug/drug";
  import {prescription,Drugdeletion,medicinealter,Medicationlook} from "../../../api/pharmacy/pharmacy";

  export default {
    name: 'PatientManagement',
    data () {
      return {
        days:false,
        // form: {
        //   id:'' ,
        //   name: '',
        //   code: '',
        //   pinyinCode: '',
        //   commonName: ''
        // },
        form: {},
        options: [{
          value: '1',
          label: '每天提醒'
        }, {
          value: '2',
          label: '间隔提醒'
        },{
        value: '3',
          label: '特殊日子'
      }],
        numberdays:[{
          value: '0',
          label: '1'
        },{
          value: '1',
          label: '2'
        },{
          value: '2',
          label: '3'
        },{
          value: '3',
          label: '5'
        },{
          value: '4',
          label: '5'
        },{
          value: '5',
          label: '6'
        }],
        optionsfry:[{
          value: '0',
          label: '1'
        },{
          value: '1',
          label: '2'
        },{
          value: '2',
          label: '3'
        },{
            value: '3',
            label: '5'
          },{
            value: '4',
            label: '5'
          }],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 选中数组
        ids: [],
        // 总条数
        total: 0,
        // 用户表格数据
        userList: [{
          time:''
        }],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          time: undefined
        },
        // 遮罩层
        loading: true,
        fage: false,
        currentPage: 1,
        pagesize: 10,
        enable: true,
        labelPosition: 'right',
        tableData: [ ],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单校验
        rules: {
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" },
            {pattern: /^[\a-\z\A-\Z0-9\u4e00-\u9fe5]+$/,required: true , message: "名称只能输入中文、数字和英文",trigger: "blur"}
          ],
          code: [
            { required: true, message: "编码不能为空", trigger: "blur" },
            {pattern:/^[a-zA-Z0-9]+$/ ,required: true , message: "编码不能为中文",trigger: "blur"}

          ],
          commonName: [
            { required: true, message: "常用名称不能为空", trigger: "blur" },
            {pattern: /^[\a-\z\A-\Z0-9\u4e00-\u9fe5]+$/,required: true , message: "常用名称只能输入中文、数字和英文",trigger: "blur"}
          ],
          pinyinCode: [
            {type:'string', required: true, message: "拼音码不能为空", trigger: "blur" },
            {pattern:/^[A-Za-z]+$/,required: true , message: "只能输入英文字母",trigger: "blur"}
          ]
        }
      }
    },
    computed:{

      // druglist(){
      //   druglist().then(res=>{
      //     console.log(res.data)
      //   }).catch(err=>{
      //     console.log(err)
      //   })
      // },
    },
    created(){
      this.druglist()
    },
    methods: {
      pin(){
        console.log(this.form.frequency)
        console.log(this.options.value)
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.druglist();
      },
      //列表
      druglist(){
        this.loading = false;
        let datas = this.$route.query.createorId
        console.log('参数',datas)
        prescription({createorId:datas} ).then(res=>{
        // druglist( this.addDateRange(this.queryParams) ).then(res=>{
          console.log('处方药列表',res)
          this.userList = res.rows;
          for (let i = 0 ; i<res.rows.length; i++){
            //遍历
            if (res.rows[i].eatDrugTimesJson != ''){
              //                                          单引号改为双引号才能遍历，/'/g是正则就是全部匹配
              let jsonStr = res.rows[i].eatDrugTimesJson.replace(/'/g, '"')
              let cons = JSON.parse(jsonStr)
              // res.rows[i].eatDrugTimesJson = cons[0].eatDrugTime  eatDrug
              res.rows[i].eatDrugTimesJson = cons[0].eatDrugQuantity
              res.rows[i].eatDrug = cons[0].eatDrugTime
              console.log(res.rows[i].eatDrug)
            };
            //
            if(res.rows[i].frequency == '1'){
              res.rows[i].frequency="每天提醒"
            }else if(res.rows[i].frequency =='2'){
              res.rows[i].frequency="间隔提醒"
            }else if(res.rows[i].frequency == '3'){
              res.rows[i].frequency="特殊日子"
            }else if(res.rows[i].frequency == ''){
              res.rows[i].frequency=''
            }
          }
          this.total = res.total;
          // this.loading = false;
        }).catch(err=>{
          console.log(err)
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.name = ''
        this.queryParams.time = ''
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      addition () {
        this.fage = true
      },
      abrogate () {
        this.fage = false
        this.$refs.form.resetFields()
      },
      details (index) {
        console.log(1)
        console.log(index)
      },
      handleSizeChange (val) {
        this.pagesize = val
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange (val) {
        this.currentPage = val
        console.log(`当前页: ${val}`)
      },
      tableRowClassName ({ row, rowIndex }) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      } ,
      /** 删除按钮操作 */
      handleDelete(row) {
        //
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          for (var i=0; i<this.ids.length;i++){
            let userIds = this.ids[i]
            Drugdeletion({id:userIds}).then(res=>{
              console.log(res)
              this.druglist()
              this.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
            }).catch(err=>{
              console.log(err)
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        getDetail(0).then(response => {
          this.open = true;
          this.title = "修改药品";
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.fage = true
        const ids = row.id || this.ids[0];
        console.log(ids)
        Medicationlook({drugId:ids}).then(res=>{
          console.log('修改详情',res)
          let bian = res.data.eatDrugTimesJson.replace(/'/g, '"')
          let conse = JSON.parse(bian)
          res.data.eatDrugTimesJson = conse[0].eatDrugQuantity
          res.data.eatDrug=conse[0].eatDrugTime
          console.log(res.data.frequency)
          if (res.data.frequency == '1'){
            res.data.frequency = '每天提醒'
          }else if(res.data.frequency == '2'){
            res.data.frequency = '间隔提醒'
          }else if(res.data.frequency == '3'){
            res.data.frequency = '特殊日子'
          }
          this.form = res.data
        }).catch(err=>{
          console.log('修改详情错误',err)
        })
      },
      // 修改确认按钮
      notarize(){
        medicinealter(this.form).then(res=>{
          console.log(res)
        }).catch(err=>{
          console.log(err)
        })
      },
      // 表单重置
      reset() {
        this.form = {
          id:undefined,
          name: undefined,
          code: undefined,
          pinyinCode: undefined,
          commonName: undefined
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateDrug(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.druglist();
              });
            } else {
              addDrug(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.druglist();
              });
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

    }
  }
</script>

<style scoped lang="scss">
  .alter{
    background: #FFFFFF;
    box-shadow: 0px 0px 32px 0px rgba(97,97,97,0.1);
    border-radius: 8px;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 10px;
    bottom: 0;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index: 10;
    overflow: hidden;
    .hint{
      width: 100%;
      height: 40px;
      line-height: 40px;
      background: #F8F8F8;
      border-radius: 8px 8px 0px 0px;
      overflow: hidden;
      p{
        float: left;
        margin-left: 20px;
        margin-top: -1px;
      }
      i{
        float: right;
        margin-right: 20px;
        margin-top: 12px;
      }
    }
  }
  .submit{
    width: 80px;
    height: 30px;
    background: #009945;
    border-radius: 4px;
    color: #ffffff;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
    float: left;
    cursor:pointer;
  }
  .cancel{
    width: 80px;
    float: left;
    height: 30px;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
    font-size: 12px;
    border: 1px solid #EEEEEE;
    margin-left: 20px;
    cursor:pointer;
  }
  .redact{
    width: 600px;
    height: 580px;
    background: #FFFFFF;
    box-shadow: 0px 0px 32px 0px rgba(97,97,97,0.1);
    border-radius: 8px;
    position:fixed;
    top: 50%;
    left: 50%;
    margin-left: -300px;
    margin-top: -285px;
    z-index: 10;
    .title{
      width: 100%;
      height: 40px;
      background: #e7e5e5;
      /*background:red;*/
      border-radius: 8px 8px 0px 0px;
    }
  }
  .search{
    width: 100%;
    height: 120px;
    background: #ffffff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0px 4px 20px 0px rgba(51,51,51,0.08);
    margin-top: 20px;
  }
  .list{
    width: 100%;
    height: 670px;
    background: #ffffff;
    box-shadow: 0px 4px 20px 0px rgba(51,51,51,0.08);
    border-radius: 8px;
    margin-top: 20px;
    position: relative;
    overflow: hidden;
  }
  .int{
    width: 40%;
    float: left;
    input{
      padding: 0px 10px;
      width: 225px;
      height: 30px;
      background: #FFFFFF;
      border: 1px solid #E8E8E8;
      border-radius: 4px;
      margin-left: 20px;
      margin-top:20px;
    }
  }
  .inquire{
    width: 20%;
    height: 50px;
    float: left;
    margin-top: 20px;
    .el-button{
      width: 31%;
      height: 30px;
    }
  }

</style>
